﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.SqlClient

Imports VNDPOS.Entity
Imports VNDPOS.EntityView
Imports VNDPOS.Helper
Imports VNDPOS.DAO
Imports VNDPOS.Config

Namespace VNDPOS.BO
    Public Class UserBO
        Inherits UserDAO
        Public Overloads Shared Function SaveFromLocation(ByVal LocationID As String, ByVal obj As User) As Boolean
            Dim con As SqlConnection = Nothing
            Dim trans As SqlTransaction = Nothing
            Try
                con = DBHelper.GetConnection1()
                trans = con.BeginTransaction()
                Dim synched As Boolean = False
                Dim centerObj As User = findByID(obj.UserID, False)
                If centerObj Is Nothing Then
                    synched = Insert(obj, False, True, con, trans)
                Else
                    Dim diff As String = centerObj.compareAndGetDiff(obj)
                    If diff <> "" Then
                        Update(obj, True, con, trans)
                    End If
                    synched = True
                End If
                If synched Then
                    DataSynchronizeBO.DeleteByRecordID("", _TableName, obj.UserID, con, trans)
                    DataSynchronizeBO.InsertRecordForLocations(obj.CompanyID, _TableName, obj.UserID, con, trans)
                    DataSynchronizeBO.DeleteByRecordID(LocationID, _TableName, obj.UserID, con, trans)
                End If
                trans.Commit()
                Return synched
            Catch ex As Exception
                If trans IsNot Nothing Then
                    trans.Rollback()
                End If
                Throw New Exception("error: save from location -> " & ex.Message, ex)
            Finally
                If con IsNot Nothing Then
                    con.Close()
                    con.Dispose()
                End If
                If trans IsNot Nothing Then
                    trans.Dispose()
                End If
            End Try
            Return False
        End Function
        Public Shared Function Save(ByVal obj As User, ByVal bPassEncrypted As Boolean) As Boolean
            Return SaveOrClone(obj, bPassEncrypted, True)
        End Function
        Public Shared Function Clone(ByVal obj As User, ByVal bPassEncrypted As Boolean) As Boolean
            Return SaveOrClone(obj, bPassEncrypted, False)
        End Function
        Protected Shared Function SaveOrClone(ByVal obj As User, ByVal bPassEncrypted As Boolean, ByVal bSave As Boolean) As Boolean
            Try
                If obj.UserID Is Nothing Then
                    Return Insert(obj, True, bPassEncrypted, bSave)
                Else
                    If findByID(obj.UserID, False) Is Nothing Then
                        Return Insert(obj, False, bPassEncrypted, bSave)
                    Else
                        Return Update(obj, bPassEncrypted, bSave)
                    End If
                End If
            Catch ex As Exception
                Throw New Exception("error: save user -> " & ex.Message, ex)
            End Try
        End Function
        Protected Overloads Shared Function Insert(ByVal obj As User, ByVal iAutoKey As Boolean, ByVal bPassEncrypted As Boolean, ByVal bSave As Boolean) As Boolean
            Dim con As SqlConnection = Nothing
            Dim trans As SqlTransaction = Nothing
            Try
                con = DBHelper.GetConnection1()
                trans = con.BeginTransaction()
                If Insert(obj, iAutoKey, bPassEncrypted, con, trans) And bSave Then
                    SystemTrackingDAO.InsertTracking(_TableName, obj.UserID, "INSERTED: " & obj.Username, obj.ModifiedID, con, trans)
                    If CONFIGAPP.WEBAPP Then
                        DataSynchronizeBO.InsertRecordForLocations(obj.CompanyID, _TableName, obj.UserID, con, trans)
                    Else
                        DataSynchronizeBO.Insert(CONFIGAPP.LOCATION_ID, _TableName, obj.UserID, con, trans)
                    End If
                End If
                trans.Commit()
                Return True
            Catch ex As Exception
                If trans IsNot Nothing Then
                    trans.Rollback()
                End If
                Throw New Exception("error: insert user -> " & ex.Message, ex)
            Finally
                If con IsNot Nothing Then
                    con.Close()
                    con.Dispose()
                End If
                If trans IsNot Nothing Then
                    trans.Dispose()
                End If
            End Try
            Return False
        End Function
        Protected Overloads Shared Function Update(ByVal obj As User, ByVal bPassEncrypted As Boolean, ByVal bSave As Boolean) As Boolean
            Dim con As SqlConnection = Nothing
            Dim trans As SqlTransaction = Nothing
            Try
                Dim oldObj As User = findByID(obj.UserID, True)
                Dim diff As String = oldObj.compareAndGetDiff(obj)
                If diff <> "" Then
                    con = DBHelper.GetConnection1()
                    trans = con.BeginTransaction()
                    If Update(obj, bPassEncrypted, con, trans) And bSave Then
                        SystemTrackingDAO.InsertTracking(_TableName, obj.UserID, "UPDATED: " & diff, obj.ModifiedID, con, trans)
                        If CONFIGAPP.WEBAPP Then
                            DataSynchronizeBO.InsertRecordForLocations(obj.CompanyID, _TableName, obj.UserID, con, trans)
                        Else
                            DataSynchronizeBO.Insert(CONFIGAPP.LOCATION_ID, _TableName, obj.UserID, con, trans)
                        End If
                    End If
                    trans.Commit()
                End If
                Return True
            Catch ex As Exception
                If trans IsNot Nothing Then
                    trans.Rollback()
                End If
                Throw New Exception("error: update user -> " & ex.Message, ex)
            Finally
                If con IsNot Nothing Then
                    con.Close()
                    con.Dispose()
                End If
                If trans IsNot Nothing Then
                    trans.Dispose()
                End If
            End Try
            Return False
        End Function
        Public Overloads Shared Function ChangePassword(ByVal obj As User) As Boolean
            Dim con As SqlConnection = Nothing
            Dim trans As SqlTransaction = Nothing
            Try
                Dim oldObj As User = findByID(obj.UserID, True)
                Dim diff As String = oldObj.compareAndGetDiff(obj)
                If diff <> "" Then
                    con = DBHelper.GetConnection1()
                    trans = con.BeginTransaction()
                    If ChangePassword(obj, con, trans) Then
                        SystemTrackingDAO.InsertTracking(_TableName, obj.UserID, "CHANGED PASSWORD", obj.ModifiedID, con, trans)
                        If CONFIGAPP.WEBAPP Then
                            DataSynchronizeBO.InsertRecordForLocations(obj.CompanyID, _TableName, obj.UserID, con, trans)
                        Else
                            DataSynchronizeBO.Insert(CONFIGAPP.LOCATION_ID, _TableName, obj.UserID, con, trans)
                        End If
                    End If
                    trans.Commit()
                End If
                Return True
            Catch ex As Exception
                If trans IsNot Nothing Then
                    trans.Rollback()
                End If
                Throw New Exception("error: change user password -> " & ex.Message, ex)
            Finally
                If con IsNot Nothing Then
                    con.Close()
                    con.Dispose()
                End If
                If trans IsNot Nothing Then
                    trans.Dispose()
                End If
            End Try
            Return False
        End Function
        Public Overloads Shared Function DeleteByID(ByVal _ID As String) As Boolean
            Dim con As SqlConnection = Nothing
            Dim trans As SqlTransaction = Nothing
            Try
                con = DBHelper.GetConnection1()
                trans = con.BeginTransaction()
                Dim obj As User = findByID(_ID, True)
                If DeleteByID(_ID, con, trans) Then
                    SystemTrackingDAO.InsertTracking(_TableName, obj.UserID, "DELETED: " & obj.ToString, obj.ModifiedID, con, trans)
                    If CONFIGAPP.WEBAPP Then
                        DataSynchronizeBO.InsertRecordForLocations(obj.CompanyID, _TableName, obj.UserID, con, trans)
                    Else
                        DataSynchronizeBO.Insert(CONFIGAPP.LOCATION_ID, _TableName, obj.UserID, con, trans)
                    End If
                End If
                trans.Commit()
                Return True
            Catch ex As Exception
                If trans IsNot Nothing Then
                    trans.Rollback()
                End If
                Throw New Exception("error: delete user -> " & ex.Message, ex)
            Finally
                If con IsNot Nothing Then
                    con.Close()
                    con.Dispose()
                End If
                If trans IsNot Nothing Then
                    trans.Dispose()
                End If
            End Try
            Return False
        End Function
    End Class
End Namespace
